package com.javasm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.javasm.domin.criteria.TotalMarketCriteria;
import com.javasm.domin.entity.CountGoods;
import com.javasm.domin.entity.Indent;
import com.javasm.domin.entity.TotalMarket;
import com.javasm.domin.entity.goods.Goods;
import com.javasm.domin.vo.IndentGoodsVo;
import com.javasm.domin.vo.TotalMarketVo;
import com.javasm.domin.vo.goods.GoodsVO;
import com.javasm.mapper.CountGoodsMapper;
import com.javasm.service.CountGoodsService;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.transfer.goods.GoodsTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author longjun
 * @since 2022-01-18
 */
@Service
@Transactional
@RequiredArgsConstructor
public class CountGoodsServiceImpl extends BaseServiceimpl<CountGoods> implements CountGoodsService {

    private final CountGoodsMapper countGoodsMapper;
    private final GoodsTransfer goodsTransfer;

    @Override
    public List<IndentGoodsVo> searchCountGoods(Long id) {
        //去重前集合
        List<IndentGoodsVo> indentGoodsVos = new ArrayList<>();

        //根据订单id查询商品集合
        List<CountGoods> countGoodsList = countGoodsMapper.findCountGoodsById(id);
        System.out.println("countGoodsList===" + countGoodsList);
        for (int i = 0; i < countGoodsList.size(); i++) {
            IndentGoodsVo indentGoodsVo = new IndentGoodsVo();
            //查询相同商品的数量
            Long goodsId = countGoodsList.get(i).getGoodsId();
//            System.out.println(goodsId);
            Long indentId = Long.valueOf(countGoodsList.get(i).getId());
//            System.out.println("indentId===="+indentId);
            Integer count = countGoodsMapper.findCount(goodsId, indentId);
//            System.out.println("count=="+count);
            //给indentGoodsVo赋值
            indentGoodsVo.setGoodsId(goodsId);
            indentGoodsVo.setGoodsName(countGoodsList.get(i).getGoodsName());
            indentGoodsVo.setSalePrice(countGoodsList.get(i).getSalePrice());
            indentGoodsVo.setGoodsNumber(count);
            indentGoodsVo.setTotalMoney(indentGoodsVo.getSalePrice() * indentGoodsVo.getGoodsNumber());
            System.out.println(indentGoodsVo);
            indentGoodsVos.add(indentGoodsVo);
        }
        System.out.println("去重前IndentGoodsVos====" + indentGoodsVos);
        //去重后集合
        List<IndentGoodsVo> indentGoodsVoList = new ArrayList<>(new HashSet<>(indentGoodsVos));
        System.out.println("去重后indentGoodsVoList==" + indentGoodsVoList);
        return indentGoodsVoList;
    }

    @Override
    public List<TotalMarketVo> searchTotalGoods() {

        //查询销售订单表中的所有id（去除重复id）
        List<CountGoods> GoodsIdList = countGoodsMapper.findGoodsidList();
        List<CountGoods> GoodsIdList2 = new ArrayList<>(new HashSet<>(GoodsIdList));
        List<TotalMarketVo>  totalMarketVoList = new ArrayList<>();
        for (CountGoods countGoods : GoodsIdList2) {
            //实体化TotalMarket对象
            TotalMarketVo totalMarkets = new TotalMarketVo();
            //去重后根据id查询数量
            Integer count = countGoodsMapper.numberGoods(countGoods.getGoodsId());
            //根据商品id查询需要显示的字段
            List<Goods> goodsList = countGoodsMapper.searchGoods(countGoods.getGoodsId());
            //转换
            List<GoodsVO> goodsVOS = goodsTransfer.setInfo(goodsList);
            totalMarkets.setGoodsId(goodsVOS.get(0).getGoodsId());
            totalMarkets.setProductColorId(goodsVOS.get(0).getProductColorId());
            totalMarkets.setGoodsName(goodsVOS.get(0).getGoodsName());
            totalMarkets.setBrandId(goodsVOS.get(0).getBrandId());
            totalMarkets.setProductId(goodsVOS.get(0).getProductId());
            totalMarkets.setTypeId(goodsVOS.get(0).getTypeId());
            totalMarkets.setTotalGoods(count);
            //添加进totalMarketVoList
            totalMarketVoList.add(totalMarkets);
        }
        return totalMarketVoList;
    }


}
